/**
 * 获取token 可判断是否登录
 * @param {为true时未登录将自动跳转至登录页面} nav 
 * @param {为true时关闭登录页将返回首页} backIndex
 * @param {是否可从登录页返回} canBack
 * @returns 
 */
export const getToken = (nav = true, backIndex = false, canBack = false) => {
	let accessToken = uni.getStorageSync('accessToken');
	if (accessToken) {
		return accessToken;
	}
	let closeType = ""
	if (backIndex) {
		closeType = "1"
	}
	if (nav) {
		if (canBack) {
			uni.navigateTo({
				url: `/pages/auth/login/login?closeType=${closeType}&canBack=${canBack}`
			})
		} else {
			uni.reLaunch({
				url: `/pages/auth/login/login?closeType=${closeType}&canBack=${canBack}`
			})
		}

	}
	return "";
}

/**
 * 是否登录
 * @param {为true时未登录将自动跳转至登录页面} nav 
 * @param {为true时关闭登录页将返回首页} backIndex
 * @param {是否可从登录页返回} canBack
 * @returns 
 */
export const isLogin = (nav = true, backIndex = false, canBack = false) => {
	return getToken(nav, backIndex, canBack);
}

// 获取用户信息 可判断是否登录 参数nav为true时未登录将自动跳转至登录页面
export const getUserInfo = (nav = true) => {
	let userInfo = uni.getStorageSync('userInfo');
	if (userInfo) {
		return userInfo;
	}
	if (nav) {
		uni.navigateTo({
			url: '/pages/auth/login/login'
		})
	}
	return "";
}

// toast消息
export const msg = (title = '', params = {}) => {
	if (!title) {
		return;
	}
	uni.showToast({
		title,
		duration: params.duration || 1500,
		mask: params.mask || false,
		icon: params.icon || 'none',
		position: params.position || 'center'
	});
}

// loading
export const showLoading = (title = '加载中', mask = true) => {
	uni.showLoading({
		title,
		mask: mask
	});
}

// hideLoading
export const hideLoading = () => {
	uni.hideLoading();
}

// 判断手机号
export const isMobile = (mobile) => {
	let reg = /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/;
	if (!reg.test(mobile)) {
		return false;
	}
	return true;
}

// 获取文件大小
export const getFileSize = (size) => {

	if (!size)
		return "0 KB";

	var num = 1024.00;

	if (size < num)
		return size + " Byte";
	if (size < Math.pow(num, 2))
		return (size / num).toFixed(2) + " KB";
	if (size < Math.pow(num, 3))
		return (size / Math.pow(num, 2)).toFixed(2) + " MB";
	if (size < Math.pow(num, 4))
		return (size / Math.pow(num, 3)).toFixed(2) + " GB";
	return (size / Math.pow(num, 4)).toFixed(2) + " TB";
}
